package linkedlist.other;

import linkedlist.Node;
import linkedlist.create.LoopLinkedCreator;

import java.util.Arrays;

/**
 * @Author wangtengyu
 * @Create 2018-09-04-15:24 查找单链表中的节点个数
 */
public class FindNodeCount {

  public int getNodeCount(Node head) {
    int count = 0;
    Node countNode = head;
    while (countNode != null) {
      countNode = countNode.getNext();
      count++;
    }
    return count;
  }

  public static void main(String[] args) {
    LoopLinkedCreator creator = new LoopLinkedCreator();
    Node node = creator.createlinkedList(Arrays.asList(1, 2, 3, 4, 5));
    FindNodeCount findNodeCount = new FindNodeCount();
    int count = findNodeCount.getNodeCount(node);
    System.out.println("链表的节点数量为" + count);
  }
}
